package com.example.gateway.interceptor;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class RequestReplaceFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Object wrapperAttr = request.getAttribute("requestWrapper");

        if (wrapperAttr != null && wrapperAttr instanceof HeaderMapRequestWrapper) {
            chain.doFilter((HeaderMapRequestWrapper) wrapperAttr, servletResponse);
        } else {
            chain.doFilter(servletRequest, servletResponse);
        }
    }
}
